package com.yb.bussiness.global;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.yb.base.dao.BaseDao;
import com.yb.base.dao.IBaseDao;
import com.yb.domain.member.TsSysuser;
import com.yb.util.comm.DateTimeUtil;
import com.yb.util.framework.PageUtil;

public class SysUserServiceImpl implements SysUserService {

	private IBaseDao<TsSysuser, Long> dao;

	public SysUserServiceImpl() {
		dao = new BaseDao<TsSysuser, Long>(TsSysuser.class);
	}

	public TsSysuser checkLogin(String username, String password) {
		String sql = "select * from t_s_sysuser where username = ? and password = ? ";
		TsSysuser user = dao.uniqueResult(sql, new Object[] { username,
				password });
		return user;
	}
    public  void  deleteUserByLoginName(String uername){
    	  dao.delete(" delete from t_s_sysuser where username=?", new Object[]{uername});
    }
	public void changePassword(String password, String username) {
		String sql = "update t_s_sysuser set password = ? where username = ? ";
		dao.update(sql, new Object[] { password, username });
	}

	public TsSysuser checkRegister(String username) {
		String sql = "select * from t_s_sysuser where username = ?  ";
		TsSysuser user = dao.uniqueResult(sql, new Object[] { username });
		return user;
	}
	public TsSysuser checkReRegister(String username,String oldusername){
		String sql = "select * from t_s_sysuser where username = ?  and  username <> ?  ";
		TsSysuser user = dao.uniqueResult(sql, new Object[] { username,oldusername });
		return user;
	}
	public TsSysuser findByUserId(int id) {
		String sql = "select * from t_s_sysuser where id = ?  ";
		TsSysuser user = dao.uniqueResult(sql, new Object[] { id });
		return user;
	}
	public boolean saveOrUpdate(TsSysuser sysUser){
		try{
		  dao.saveOrUpdate(sysUser);
		  return true;
		}catch(Exception e)
		{
			e.printStackTrace();
			return false;
		}
		
	}

	public Long saveUser(TsSysuser user) {
		return dao.saveEntity(user);
	}

	public void changeModulePassword(String password, String username) {
		String sql = "update t_s_sysuser set modulePassword = ? where username = ? ";
		dao.update(sql, new Object[] { password, username });
	}

	public List<TsSysuser> checkUserNameIsExistMember(String username) {
		return dao.findList("select * from t_s_sysuser where username = ?",
				new Object[] { username });
	}

	public boolean deleteUser(String idString) {
		boolean isFlag = true;
		if (idString == null || "".equals(idString))
			return false;
		String sql = "";
		String[] delteid = idString.split("_");
		try {
			for (int i = 0; i < delteid.length; i++) {
				sql = "delete from t_s_sysuser where id=" + delteid[i];
				dao.delete(sql);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return isFlag;
	}

	public PageUtil getList(Map<String, String> map, int start,
			int limit) {
		if (map == null || map.size() == 0)
			return null;
			PageUtil pu = new PageUtil();
			String find_sql ="SELECT * FROM t_s_sysuser where 1=1 ";
			String countSql ="SELECT count(*) FROM t_s_sysuser where 1=1 ";
			try {
				if (null != map.get("userName") && !"".equals(map.get("userName"))) {
					find_sql += "  and userName like '%" + map.get("userName") + "%'";
					countSql += "  and userName like '%" + map.get("userName") + "%'";
				}
				
				if (null != map.get("type") && !"".equals(map.get("type"))) {
					find_sql += "  and type = '" + map.get("type") + "'";
					countSql += "  and type = '" + map.get("type") + "'";
				}
				
				List<Object[]> countList = dao.findObjectArryList(countSql.toString());
				List<TsSysuser> newlist = null;
				if (null != countList && countList.size() > 0) {
					Object[] countAry = countList.get(0);
					pu.setTotalRecords(new Integer(countAry[0].toString()));
					List<TsSysuser> list = dao.findList(find_sql+" ORDER BY createDate desc limit ?,?",new Object[]{(start-1)*limit,limit});
					if (list != null && list.size() > 0) {
						newlist = new ArrayList<TsSysuser>();
						for (TsSysuser tp : list) {
							if(null!=tp.getCreateDate())
							tp.setCreateDateString(DateTimeUtil.switchDateToString(tp.getCreateDate(), "yyyy-MM-dd HH:mm:ss"));
							newlist.add(tp);
						}
					}
				} else {
					pu.setTotalRecords(0);
				}
				if (null == newlist)
					newlist = new ArrayList<TsSysuser>();
				pu.setList(newlist);
				pu.setPageNo(start);
				pu.setPageSize(limit);
				return pu;
			} catch (Exception e) {
				e.printStackTrace();
			}
			return null;
			}

	public List<TsSysuser> loadYzByTime(String time) {
		String sql = "SELECT s.username,m.realName password,m.phone type,m.address modulePassword,s.createDate FROM t_s_sysuser s,t_s_member m  WHERE  s.id = m.sysuserId AND createDate > '"+time+"' AND TYPE = 4";
		List<TsSysuser> list = dao.findList(sql);
		return list;
	}

	public List<TsSysuser> loadSjByTime(String time) {
		String sql = "SELECT s.username,m.name password,m.phone type,m.shopAddress modulePassword,m.companyOPerer source,s.createDate FROM t_s_sysuser s,t_g_brand m  WHERE  s.id = m.sysuserId AND createDate > '"+time+"' AND TYPE = 6";
		List<TsSysuser> list = dao.findList(sql);
		return list;
	}

	public String resetPassword(String userName, String realName,
			String userType) {
		String sql = "";
		if("1".equals(userType))
			sql = "select count(s.id) as id from t_s_sysuser s , t_s_member m where s.id = m.sysuserId and s.username=? and m.realName=?";
		if("2".equals(userType))
			sql = "select count(s.id) as id from t_s_sysuser s, t_g_brand b where s.id = b.sysuserId and s.username=? and b.name=?";
		List<TsSysuser> list = dao.findList(sql,new Object[]{userName,realName});
		int count = new Integer(list.get(0).getId().toString());
		if(count>0){
			try {
				if("1".equals(userType))
					sql = "update t_s_sysuser s set password='MDAwMDAw' where username=? and EXISTS (select 1 from t_s_member m where s.id = m.sysuserId and m.realName=?)";
				if("2".equals(userType))
					sql = "update t_s_sysuser s set password='MDAwMDAw' where username=? and EXISTS (select 1 from t_g_brand b where s.id = b.sysuserId and b.name=?)";
				dao.update(sql, new Object[] { userName, realName });
				return "1";
			} catch (RuntimeException e) {
				return "2";
			}
			
		}
		return "0";
	}
	
}
